﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EmotionRecognitionMessages;
using EmotionRecognitionEntities;

namespace EmotionRecognitionGui
{//
    public partial class TakeImage : ImageAnalysis
    {
        GuiManager guiManager;
        EmotionRecognitionEntities.WebCam webcam;
        public TakeImage()
        {
            try
            {
                guiManager = GuiManager.Instance;
                InitializeComponent();
                base.DisposeObjectsInherent();
                webcam = new EmotionRecognitionEntities.WebCam();
                webcam.InitializeWebCam(this.imgVideo);
                imgCapture.Visible = false;
                webcam.Start();
                webcam.Stop();
            }
            catch (Exception e)
            {
                MessageBox.Show("Error: " + e.ToString());
            }
        }
        public void EnterTakeImage()
        {
            webcam.Continue();
        }
        public void ExitTakeImage()
        {
            webcam.Stop();
        }
        //protected void TakeImage_Load(object sender, EventArgs e)
        //{
        //    webcam = new EmotionRecognitionEntities.WebCam();
        //    webcam.InitializeWebCam(this.imgVideo);
        //    imgCapture.Visible = false;
        //    webcam.Start();
        //}


        protected void bntContinue_Click(object sender, EventArgs e)
        {
            if (imgCapture.Visible == true && imgVideo.Visible == false)
            {
                webcam.Continue();
                imgCapture.Image = null;
                imgCapture.Visible = false;
                imgVideo.Visible = true;
            }
        }

        protected void bntCapture_Click(object sender, EventArgs e)
        {
            if (imgCapture.Visible == false && imgVideo.Visible == true)
            {
                imgCapture.Image = imgVideo.Image;
                imgVideo.Visible = false;
                webcam.Stop();
                imgCapture.Visible = true;
            }
        }

        protected void bntSave_Click(object sender, EventArgs e)
        {
            if (imgCapture.Visible == true && imgVideo.Visible == false)
            {
                Messages saveMsg = new Messages();
                Messages nameMsg = new Messages();
                Messages saveDBMsg = new Messages();
                ReplyMsg nameRpy = nameMsg.getExternalImageName();
                ReplyMsg saveRpy = saveMsg.SaveImage(imgCapture.Image, Environment.GetEnvironmentVariable("ExternalPictures"), (string)nameRpy.ParamArray[0] + ".jpg");
                if (saveRpy.MessageState == true && nameRpy.MessageState == true)
                {
                    imgCapture.Visible = true;
                    imgVideo.Visible = false;
                }

                saveDBMsg.CreateNewPicutreInDB((string)nameRpy.ParamArray[0] + ".jpg", (int)GuiManager.Folders.Externals);
            }
        }

        protected void bntVideoFormat_Click(object sender, EventArgs e)
        {
            webcam.ResolutionSetting();//
        }

        protected void bntVideoSource_Click(object sender, EventArgs e)
        {
            webcam.AdvanceSetting();
        }

        
        new protected void buttonBack_Click(object sender, EventArgs e)
        {
            this.ExitTakeImage();
            guiManager.SwitchForms(this, guiManager.mainMenuReviewerGui);
        }

        private void buttonDataBaseImages_Click_1(object sender, EventArgs e)
        {
            this.ExitTakeImage();
        }

    }
    
}
